package unit15.textFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class TextFileTest {
	public static void main(String[] args) throws IOException, ParseException{
		Employee[] staff=new Employee[3];
		staff[0]=new Employee("Alice", 50000, 1997,6,6);
		staff[1]=new Employee("Mike", 60000, 1998,4,4);
		staff[2]=new Employee("Ben", 70000, 1999,7,7);
		
		try (PrintWriter out=new PrintWriter("C:\\Users\\asus\\Desktop\\Employee.dat","UTF-8")){
			writeDate(staff,out);
		}
		try (Scanner in=new Scanner(new FileInputStream("C:\\Users\\asus\\Desktop\\Employee.dat"),"UTF-8")){
			Employee[] newStaff=readData(in);
			for(Employee e:newStaff){
				System.out.println(e);
			}
		}
	}

	private static Employee[] readData(Scanner in) throws ParseException {
		// TODO Auto-generated method stub
		int n=in.nextInt();
		in.nextLine();
		
		Employee[] employees=new Employee[n];
		for(int x=0;x<n;x++){
			employees[x]=readEmployee(in);
		}
		return employees;
	}
	private static Employee readEmployee(Scanner in) throws ParseException {
		// TODO Auto-generated method stub
		String line=in.nextLine();
		String[] tokens=line.split("\\-");
		String name=tokens[0];
		int salary=Integer.parseInt(tokens[1]);
		int year=Integer.parseInt(tokens[2]);
		int month=Integer.parseInt(tokens[3]);
		int day=Integer.parseInt(tokens[4]);
		return new Employee(name, salary,year,month,day);
	}

	private static void writeDate(Employee[] staff, PrintWriter out) {
		// TODO Auto-generated method stub
		out.println(staff.length);
		
		for(Employee e:staff){
			writeEmployee(out,e);
		}
	}

	private static void writeEmployee(PrintWriter out, Employee e) {
		// TODO Auto-generated method stub
		GregorianCalendar calendar=new GregorianCalendar();
		calendar.setTime(e.getDate());
		out.println(e.getUsername()+"-"+e.getSalary()+"-"+calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DATE));
	}
}
